from django.contrib import admin
from .models import (
    AIProvider, AIModel, AIRole, AIConversation, 
    AIMessage, TokenUsageLog, MessageTemplate, UserFavorite
)


@admin.register(AIProvider)
class AIProviderAdmin(admin.ModelAdmin):
    list_display = ['name', 'provider_type', 'owner', 'is_active', 'is_public', 'created_at']
    list_filter = ['provider_type', 'is_active', 'is_public', 'created_at']
    search_fields = ['name', 'owner__username']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'provider_type', 'owner', 'api_endpoint')
        }),
        ('配置信息', {
            'fields': ('api_key', 'default_params', 'is_active', 'is_public')
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(AIModel)
class AIModelAdmin(admin.ModelAdmin):
    list_display = ['name', 'model_code', 'provider', 'max_tokens', 'is_active', 'created_at']
    list_filter = ['provider', 'is_active', 'created_at']
    search_fields = ['name', 'model_code', 'provider__name']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'model_code', 'provider')
        }),
        ('配置信息', {
            'fields': ('max_tokens', 'cost_per_1k_tokens', 'model_params', 'is_active')
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(AIRole)
class AIRoleAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'created_by', 'is_public', 'usage_count', 'created_at']
    list_filter = ['category', 'is_public', 'created_at']
    search_fields = ['name', 'created_by__username']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by', 'usage_count']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'category', 'is_public')
        }),
        ('角色配置', {
            'fields': ('system_prompt',)
        }),
        ('统计信息', {
            'fields': ('usage_count',),
            'classes': ('collapse',)
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(AIConversation)
class AIConversationAdmin(admin.ModelAdmin):
    list_display = ['title', 'owner', 'ai_model', 'ai_role', 'status', 'used_tokens', 'last_message_at']
    list_filter = ['status', 'ai_model', 'ai_role', 'created_at']
    search_fields = ['title', 'owner__username']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by', 'used_tokens']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'owner', 'status')
        }),
        ('AI配置', {
            'fields': ('ai_model', 'ai_role')
        }),
        ('统计信息', {
            'fields': ('used_tokens', 'last_message_at'),
            'classes': ('collapse',)
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(AIMessage)
class AIMessageAdmin(admin.ModelAdmin):
    list_display = ['conversation', 'ai_role', 'token_count', 'created_at']
    list_filter = ['ai_role', 'created_at']
    search_fields = ['conversation__title', 'content']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('conversation', 'ai_role', 'parent_message')
        }),
        ('消息内容', {
            'fields': ('content', 'token_count', 'metadata')
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(TokenUsageLog)
class TokenUsageLogAdmin(admin.ModelAdmin):
    list_display = ['user', 'ai_model', 'input_tokens', 'output_tokens', 'total_tokens', 'created_at']
    list_filter = ['ai_model', 'created_at']
    search_fields = ['user__username', 'conversation__title']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by', 'total_tokens']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'conversation', 'message', 'ai_model')
        }),
        ('Token统计', {
            'fields': ('input_tokens', 'output_tokens', 'total_tokens')
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(MessageTemplate)
class MessageTemplateAdmin(admin.ModelAdmin):
    list_display = ['name', 'owner', 'category', 'is_public', 'usage_count', 'created_at']
    list_filter = ['category', 'is_public', 'created_at']
    search_fields = ['name', 'owner__username', 'content']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by', 'usage_count']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'owner', 'category', 'is_public')
        }),
        ('模板内容', {
            'fields': ('content', 'variables')
        }),
        ('统计信息', {
            'fields': ('usage_count',),
            'classes': ('collapse',)
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )


@admin.register(UserFavorite)
class UserFavoriteAdmin(admin.ModelAdmin):
    list_display = ['title', 'owner', 'conversation', 'message', 'created_at']
    list_filter = ['created_at']
    search_fields = ['title', 'owner__username', 'note']
    readonly_fields = ['created_at', 'updated_at', 'created_by', 'updated_by']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'owner', 'conversation', 'message')
        }),
        ('收藏内容', {
            'fields': ('note',)
        }),
        ('系统信息', {
            'fields': ('created_at', 'updated_at', 'created_by', 'updated_by'),
            'classes': ('collapse',)
        })
    )
